package com.xhx.accounting;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class accountbook extends AppCompatActivity {
    private DBDao db;
    protected List<Map<String,Object>> mAccountbook;
    private ListView account_lv;
    private ListViewAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accountbook);
        db = new DBDao(accountbook.this);
        mAccountbook = db.getAccountBook();
        account_lv = findViewById(R.id.account_lv);

        mAdapter = new ListViewAdapter(this,mAccountbook);
        account_lv.setAdapter(mAdapter);

        account_lv.setOnItemClickListener((parent, view, position, id) -> {
            HashMap<String, Object> listinfo = (HashMap<String, Object>) account_lv.getItemAtPosition(position);//SimpleAdapter返回Map
            Log.d("onClick",listinfo.toString());
            int accountBookId = (int)listinfo.get("id");
            db.changeCurrentAccountBook(accountBookId);
            finish();
        });

        account_lv.setOnItemLongClickListener((parent, view, position, id) -> {
            HashMap<String, Object> listinfo = (HashMap<String, Object>) account_lv.getItemAtPosition(position);//SimpleAdapter返回Map
            Log.d("onLongClick",listinfo.toString());
            int accountBookId = (int)listinfo.get("id");
            if(accountBookId!=1){
                AlertDialog.Builder builder = new AlertDialog.Builder(accountbook.this);
                builder.setMessage("此账本下的记录不会被删除且此操作不可撤销")
                        .setTitle("确定删除此账本吗？");
                builder.setPositiveButton("确认", (dialog, id1) -> {
                    // User clicked OK button
                    db.deleteAccountBook(accountBookId);
                    mAccountbook.clear();
                    mAccountbook.addAll(db.getAccountBook());
                    mAdapter.notifyDataSetChanged();
                });
                builder.setNegativeButton("取消", (dialog, id12) -> {
                    // User cancelled the dialog
                });
                builder.show();
            }
            return true;
        });

        ImageView fab = findViewById(R.id.add_account);
        fab.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setClass(accountbook.this, add_accountbook.class);
            startActivity(intent);
        });

    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.arrow_left:
                finish();
                break;
        }
    }

    public void onStart(){
        super.onStart();
        mAccountbook.clear();
        mAccountbook.addAll(db.getAccountBook());
        mAdapter.notifyDataSetChanged();
    }


}